//
//  WDMySubscribeViewController.m
//  winterTest
//
//  Created by wd on 15/10/26.
//  Copyright © 2015年 Wei Chuang Le ,Ltd. All rights reserved.
//

#import "WDMySubscribeViewController.h"

@interface WDMySubscribeViewController () //<UITableViewDelegate,UITableViewDataSource>

/** baseView */
@property (nonatomic, weak) UIView *baseView;

/** imageview */
@property (nonatomic, weak) UIImageView *imageView;

/**  */
@property (nonatomic, strong) NSMutableArray *imageArray;

/**  */
@property (nonatomic, getter=isRequested) BOOL requested;
/**  */
@property (nonatomic, assign) NSInteger index;

@end

@implementation WDMySubscribeViewController

- (NSMutableArray *)imageArray
{
    if (nil == _imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.index = 0;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];
    imageView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    UIButton *preBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [preBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [preBtn setTitle:@"上一张" forState:UIControlStateNormal];
    [preBtn addTarget:self action:@selector(preOnclick:) forControlEvents:UIControlEventTouchUpInside];
    preBtn.frame = CGRectMake(50, CGRectGetMaxY(imageView.frame)+20, 60, 30);
    [self.view addSubview:preBtn];
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [nextBtn setTitle:@"下一张" forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextOnclick:) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.frame = CGRectMake(200, CGRectGetMinY(preBtn.frame), 60, 30);
    [self.view addSubview:nextBtn];
    
    UIButton *funBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [funBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [funBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [funBtn setTitle:@"funBtn" forState:UIControlStateNormal];
    [funBtn addTarget:self action:@selector(funBtntOnclick:) forControlEvents:UIControlEventTouchUpInside];
    funBtn.frame = CGRectMake(130, CGRectGetMaxY(preBtn.frame)+10, 60, 30);
    [self.view addSubview:funBtn];
    
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    baseView.backgroundColor = [UIColor redColor];
    [self.view addSubview:baseView];
    self.baseView = baseView;
    baseView.hidden = YES;
}

- (void)funBtntOnclick:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    
    self.imageView.hidden = sender.isSelected;
    self.baseView.hidden = !self.imageView.isHidden;
}

- (void)preOnclick:(UIButton *)sender
{
    self.index--;
    if (self.index < 0) {
        self.index = 0;
    }
    
    if (self.index >= self.imageArray.count) {
        return;
    }
    self.imageView.image = self.imageArray[self.index];
    
    CATransition *ca = [CATransition animation];
    //  动画的开始与结束的快慢
    ca.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    ca.type = @"pageCurl"; // 动画效果
    ca.subtype = kCATransitionFromLeft; // 动画方向
    ca.duration = 2.0; // 动画时间
    
    self.view.backgroundColor = [UIColor greenColor];
}

- (void)nextOnclick:(UIButton *)sender
{
    self.index++;
    if (self.index > 3) {
        self.index = 3;
    }
    
    if (self.index >= self.imageArray.count) {
        return;
    }
    self.imageView.image = self.imageArray[self.index];
    
    CATransition *ca = [CATransition animation];
    ca.type = @"pageUnCurl";
    ca.subtype = kCATransitionFromRight;
    ca.duration = 2.0;
    
    [self.imageView.layer addAnimation:ca forKey:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    if (!self.imageView.isHidden) {
        NSArray *urls = @[
                          @"http://pica.nipic.com/2008-03-19/2008319183523380_2.jpg",
                          @"http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg",
                          @"http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg",
                          @"http://img2.3lian.com/img2007/19/33/005.jpg"
                          ];
        
        for (NSString *aUrlString in urls) {
            [self performSelectorInBackground:@selector(startDownload:) withObject:aUrlString];
        }
    } else {
        
        // 核心动画
        
        // 平移动画
        CABasicAnimation *basicAnimation1 = [CABasicAnimation animation];
        basicAnimation1.keyPath = @"transform.translation.x";
        basicAnimation1.toValue = @(100);
        
        CABasicAnimation *basicAnimation2 = [CABasicAnimation animation];
        basicAnimation2.keyPath = @"transform.translation.y";
        basicAnimation2.toValue = @(300);
        
        // 缩放动画
        CABasicAnimation *basicAnimation3 = [CABasicAnimation animation];
        basicAnimation3.keyPath = @"transform.scale";
        basicAnimation3.toValue = @(0.0);
        
        // 旋转动画
        CABasicAnimation *basicAnimation4 = [CABasicAnimation animation];
        basicAnimation4.keyPath = @"transform.rotation";
        basicAnimation4.toValue = @(4*M_PI);
        
        // 组动画
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        animationGroup.animations = @[basicAnimation1, basicAnimation2, basicAnimation3, basicAnimation4];
        
        // 设置组动画时间
        animationGroup.duration = 1;
        animationGroup.fillMode = kCAFillModeForwards;
        animationGroup.removedOnCompletion = NO;
        
        [self.baseView.layer addAnimation:animationGroup forKey:nil];
    }
}

- (void)startDownload:(NSString *)urlString
{
    NSURL *URL = [NSURL URLWithString:urlString];
    NSData *imageData = [NSData dataWithContentsOfURL:URL];
    
    UIImage *image = [UIImage imageWithData:imageData];
    
    [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];
}

- (void)settingImage:(UIImage *)image
{
    if (self.isRequested) {
        return;
    }
    self.imageView.image = image;
    [self.imageArray addObject:image];
    
    if (4 == self.imageArray.count) {
        self.requested = YES;
    }
}

@end
